package com.scheduled.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.net.UnknownHostException;


/**
 * @description:
 * @author: chenjianshuo
 * @create: 2019-09-04 16:42
 **/

@Configuration
public class RedisConfig {

	@Value("${spring.redis.jedis.pool.max-active}")
	private Integer maxTotal;
	@Value("${spring.redis.jedis.pool.max-wait}")
	private Long maxWaitMillis;
	@Value("${spring.redis.jedis.pool.max-idle}")
	private Integer maxIdle;
	@Value("${spring.redis.jedis.pool.min-idle}")
	private Integer minIdle;

	@Value("${spring.redis.host}")
	private String hostName;
	@Value("${spring.redis.port}")
	private Integer port;
	@Value("${spring.redis.password}")
	private String password;
	@Value("${spring.redis.database}")
	private Integer database;

	@Bean
	public JedisConnectionFactory redisConnectionFactory() {
		return RedisConfigFactory.jedisConnectionFactory(hostName, port, password, database, RedisConfigFactory.poolConfig(maxTotal, maxWaitMillis, maxIdle, minIdle));
	}

	@Bean(name = "stringRedisTemplate")
	public StringRedisTemplate stringRedisTemplate() throws UnknownHostException {
		return RedisConfigFactory.stringRedisTemplate(redisConnectionFactory());
	}

	@Bean
	@Primary
	public RedisTemplate<Object, Object> redisTemplate() throws UnknownHostException {
		return RedisConfigFactory.redisTemplate(redisConnectionFactory());
	}


}
